const {
	parentPort,
	isMainThread
} = require("worker_threads")

const puppeteer = require('puppeteer');

const fs = require('fs')
const path = require('path')
const port = 4080
const addRess = `http://127.0.0.1:${port}/`
const fileUrl = './public/Images'

parentPort.on("message", (res) => {
	(async () => {
		let browser = await puppeteer.launch({
			headless: true,
			args: [
				'-–disable-gpu',
				'-–disable-dev-shm-usage',
				'-–disable-setuid-sandbox',
				'-–no-first-run',
				'-–no-sandbox',
				'-–no-zygote',
				'-–single-process',
				'--no-default-browser-check',
				'--no-first-run',
				'--no-pings',
				'--no-sandbox',
				'--no-zygote',
				'--password-store=basic',
				'--use-mock-keychain',
			]
		})

		let dir = path.join(fileUrl, res[0] + "images")
		fs.readdir(dir, (error, data) => {
			if (!data) {
				fs.mkdirSync(dir, 77, true)
			}
		})

		// return
		for (let i = 0; i < 20; i++) {
			let page = await browser.newPage()
			await page.goto(
				`${addRess}/index.html?${res[0] + "-" + i +"-"+new Date().getTime().toString().substr(-6)}`
			)

			await page.setViewport({
				width: 800,
				height: 1080
			})

			// await page.waitForTimeout(500)

			await page.screenshot({
				path: path.join(dir, res[0] + "_" + i + '.png')
			})
			page.close()
		}
		console.log(res[0] + ":截图结束")
		browser.close()
	})()

	console.log(res)
})
